MyBatis 版本:MyBatis 3.3.0
MyBatis 官方网站(英文):MyBatis 英文官网
MyBatis 官方网站(中文):MyBatis 中文官网
MyBatis 源码:MyBatis 源码
MyBatis 配置文件层次结构
1 | <?xml version="1.0" encoding="UTF-8"> |
TypeAliases 元素
别名是当类全限定名过长时可以用一个简短的名称去指代它,且该名称可以在 MyBatis 上下文中使用。
别名分为:
- 系统定义别名
- 自定义别名
别分不分大小写,因为 TypeAliasRgistry 在辨别别名时会统一转换为小写后辨别(后续源码分析)。
别名是不允许重复的。
系统定义别名
MyBatis 源码 org.apache.ibatis.type.TypeAliasRegistry 中定义了所有的系统定义别名。
别名 | 映射的类型 | 是否支持数组 |
---|---|---|
_byte | byte | 是 |
_long | long | 是 |
_short | short | 是 |
_int | int | 是 |
_integer | int | 是 |
_double | double | 是 |
_float | float | 是 |
_boolean | boolean | 是 |
string | String | 否 |
byte | Byte | 是 |
long | Long | 是 |
short | Short | 是 |
int | Integer | 是 |
integer | Integer | 是 |
double | Double | 是 |
float | Float | 是 |
boolean | Boolean | 是 |
date | Date | 是 |
decimal | BigDecimal | 是 |
bigdecimal | BigDecimal | 是 |
object | Object | 是 |
map | Map | 否 |
hashmap | HashMap | 否 |
list | List | 否 |
arraylist | ArrayList | 否 |
collection | Collection | 否 |
iterator | Iterator | 否 |
ResultSet | ResultSet | 否 |
支持数组即可以使用类似于 date[] 这样的别名。
org.apache.ibatis.type.TypeAliasRegistry#TypeAliasRegistry 源码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52public TypeAliasRegistry() {
this.registerAlias("string", String.class);
this.registerAlias("byte", Byte.class);
this.registerAlias("long", Long.class);
this.registerAlias("short", Short.class);
this.registerAlias("int", Integer.class);
this.registerAlias("integer", Integer.class);
this.registerAlias("double", Double.class);
this.registerAlias("float", Float.class);
this.registerAlias("boolean", Boolean.class);
this.registerAlias("byte[]", Byte[].class);
this.registerAlias("long[]", Long[].class);
this.registerAlias("short[]", Short[].class);
this.registerAlias("int[]", Integer[].class);
this.registerAlias("integer[]", Integer[].class);
this.registerAlias("double[]", Double[].class);
this.registerAlias("float[]", Float[].class);
this.registerAlias("boolean[]", Boolean[].class);
this.registerAlias("_byte", Byte.TYPE);
this.registerAlias("_long", Long.TYPE);
this.registerAlias("_short", Short.TYPE);
this.registerAlias("_int", Integer.TYPE);
this.registerAlias("_integer", Integer.TYPE);
this.registerAlias("_double", Double.TYPE);
this.registerAlias("_float", Float.TYPE);
this.registerAlias("_boolean", Boolean.TYPE);
this.registerAlias("_byte[]", byte[].class);
this.registerAlias("_long[]", long[].class);
this.registerAlias("_short[]", short[].class);
this.registerAlias("_int[]", int[].class);
this.registerAlias("_integer[]", int[].class);
this.registerAlias("_double[]", double[].class);
this.registerAlias("_float[]", float[].class);
this.registerAlias("_boolean[]", boolean[].class);
this.registerAlias("date", Date.class);
this.registerAlias("decimal", BigDecimal.class);
this.registerAlias("bigdecimal", BigDecimal.class);
this.registerAlias("biginteger", BigInteger.class);
this.registerAlias("object", Object.class);
this.registerAlias("date[]", Date[].class);
this.registerAlias("decimal[]", BigDecimal[].class);
this.registerAlias("bigdecimal[]", BigDecimal[].class);
this.registerAlias("biginteger[]", BigInteger[].class);
this.registerAlias("object[]", Object[].class);
this.registerAlias("map", Map.class);
this.registerAlias("hashmap", HashMap.class);
this.registerAlias("list", List.class);
this.registerAlias("arraylist", ArrayList.class);
this.registerAlias("collection", Collection.class);
this.registerAlias("iterator", Iterator.class);
this.registerAlias("ResultSet", ResultSet.class);
}
自定义别名
类型别名是为 Java 类型设置一个短的名字。它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余。
定义自定义别名有三种方式:
- 指定类别名
1 | <typeAliases> |
- 包扫描(在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名)
1 | <typeAliases> |
- 注解
1 |
|
注解的优先级最高,若有注解,则别名是注解中指定的别名